#include "playbackwidget.h"

#include <QHBoxLayout>
#include <QVBoxLayout>

#include <QPushButton>
#include <QMediaPlaylist>


#define VIDEO_LOCAL "c:/Data/example.mp4"
#define VIDEO_REMOTE "rtsp://v8.cache1.c.youtube.com/CiILENy73wIaGQnNUH6lxLd0MRMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" 

#include "activelogs.h"
#ifdef PLAYBACK_WIDGET
    #define ENABLE_FILE_LOG
#endif
#include "logmacros.h"

PlaybackWidget::PlaybackWidget(QWidget *parent)
    : QWidget(parent)
{
	QDEBUG_ENTER_FN();
	m_player = new QMediaPlayer(this);
	connect(m_player, SIGNAL(mediaStatusChanged ( QMediaPlayer::MediaStatus )),
			this, SLOT(mediaStatusChanged ( QMediaPlayer::MediaStatus )));
	m_videoWidget = new QVideoWidget(parent);
	m_videoWidget->showMaximized();
	m_player->setVideoOutput(m_videoWidget);
	
	QHBoxLayout* layout = new QHBoxLayout(this); 
	layout->addWidget(m_videoWidget, 7);
	
	
	m_playButton = new QPushButton("play", this);
	m_pauseButton = new QPushButton("pause", this);
	m_stopButton = new QPushButton("stop", this);
	m_hideButton = new QPushButton("hide", this);
	
	connect(m_playButton, SIGNAL(clicked(bool)), m_player, SLOT(play()));
	connect(m_pauseButton, SIGNAL(clicked(bool)), m_player, SLOT(pause()));
	connect(m_stopButton, SIGNAL(clicked(bool)), m_player, SLOT(stop()));
	connect(m_hideButton, SIGNAL(clicked(bool)), m_stopButton, SIGNAL(clicked(bool)));
	
	QVBoxLayout* vlayout = new QVBoxLayout(this);
	vlayout->addStretch(1);
	vlayout->addWidget(m_playButton, 1);
	vlayout->addStretch(1);
	vlayout->addWidget(m_pauseButton, 1);
	vlayout->addStretch(1);
	vlayout->addWidget(m_stopButton, 1);
	vlayout->addStretch(1);
	vlayout->addWidget(m_hideButton, 1);
	vlayout->addStretch(1);
	
	layout->addLayout(vlayout, 3);
	
	setLayout(layout);
	m_playlist = new QMediaPlaylist();
	m_player->setPlaylist(m_playlist);
	QDEBUG_EXIT_FN();
}

PlaybackWidget::~PlaybackWidget()
{

}

void PlaybackWidget::mediaStatusChanged ( QMediaPlayer::MediaStatus status ) {
	QDEBUG_ENTER_FN();
	QDEBUG("new State" << stateToString(status));
	if(status == QMediaPlayer::InvalidMedia) {
		QDEBUG("file was" << m_player->media().canonicalUrl()<<"error was:" << m_player->errorString());
	}
	QDEBUG_EXIT_FN();
}

QString PlaybackWidget::stateToString ( QMediaPlayer::MediaStatus status ) { 
	switch(status) {
		case QMediaPlayer::UnknownMediaStatus:
			return QString("QMediaPlayer::UnknownMediaStatus");
		case QMediaPlayer::NoMedia:
			return QString("QMediaPlayer::NoMedia");
		case QMediaPlayer::LoadingMedia:
			return QString("QMediaPlayer::LoadingMedia");
		case QMediaPlayer::LoadedMedia:
			return QString("QMediaPlayer::LoadedMedia");
		case QMediaPlayer::StalledMedia:
			return QString("QMediaPlayer::StalledMedia");
		case QMediaPlayer::BufferingMedia:
			return QString("QMediaPlayer::BufferingMedia");
		case QMediaPlayer::BufferedMedia:
			return QString("QMediaPlayer::BufferedMedia");
		case QMediaPlayer::EndOfMedia:
			return QString("QMediaPlayer::EndOfMedia");
		case QMediaPlayer::InvalidMedia:
			return QString("QMediaPlayer::InvalidMedia");
	}
	return QString("unknown state!!!");
}


void PlaybackWidget::setLocalUrl() {
	QDEBUG_ENTER_FN();
	QUrl url = QUrl::fromLocalFile(VIDEO_LOCAL);
	setMediaUrl(url);
	QDEBUG_EXIT_FN();
}

void PlaybackWidget::setRemoteUrl() {
	QDEBUG_ENTER_FN();
	QUrl url(VIDEO_REMOTE);
	setMediaUrl(url);
	QDEBUG_EXIT_FN();
}

void PlaybackWidget::setMediaUrl(QUrl aUrl) {
	QDEBUG_ENTER_FN();
	QDEBUG("link to video" << aUrl);
	m_playlist->clear();
	m_playlist->addMedia(aUrl);
	QDEBUG_EXIT_FN();
}
